package com.cskaoyan.service.wx.impl;

import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.wx.goods.vo.HomeIndexVo;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.wx.WxHomeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname WxHomeServiceImpl
 * @Description
 * @Date 2021/8/14 14:17
 * @Author : xujun
 */
@Service
@Transactional
public class WxHomeServiceImpl implements WxHomeService {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    GrouponMapper grouponMapper;

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    AdMapper adMapper;

    @Autowired
    TopicMapper topicMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    SystemMapper systemMapper;


    @Override
    public HomeIndexVo index() {

        // banner
        AdExample adExample = new AdExample();
        adExample.createCriteria()
                .andDeletedEqualTo(false)
                .andEnabledEqualTo(true);
        List<Ad> ads = adMapper.selectByExample(adExample);
        List<HomeIndexVo.AdIndexVo> banner = new ArrayList<>();

        for (Ad ad : ads) {

            HomeIndexVo.AdIndexVo adIndexVo = new HomeIndexVo.AdIndexVo();
            BeanUtils.copyProperties(ad, adIndexVo);

            banner.add(adIndexVo);
        }


        // channel
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andDeletedEqualTo(false).andPidEqualTo(0);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        List<HomeIndexVo.CategoryIndexVo> channel = new ArrayList<>();

        for (Category category : categories) {

            HomeIndexVo.CategoryIndexVo categoryIndexVo = new HomeIndexVo.CategoryIndexVo();
            BeanUtils.copyProperties(category, categoryIndexVo);

            channel.add(categoryIndexVo);
        }


        // brandList
        String brandCount = systemMapper.selectByPrimaryKey(47).getKeyValue();

        BrandExample brandExample = new BrandExample();
        brandExample.setOrderByClause("id" + " limit " + brandCount);
        brandExample.createCriteria().andDeletedEqualTo(false);
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        List<HomeIndexVo.BrandIndexVo> brandList = new ArrayList<>();

        for (Brand brand : brands) {

            HomeIndexVo.BrandIndexVo brandIndexVo = new HomeIndexVo.BrandIndexVo();
            BeanUtils.copyProperties(brand, brandIndexVo);

            brandList.add(brandIndexVo);
        }


        // couponList
        CouponExample couponExample = new CouponExample();
        couponExample.createCriteria().andDeletedEqualTo(false);
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        List<HomeIndexVo.CouponIndexVo> couponList = new ArrayList<>();

        for (Coupon coupon : coupons) {
            HomeIndexVo.CouponIndexVo couponIndexVo = new HomeIndexVo.CouponIndexVo();
            BeanUtils.copyProperties(coupon, couponIndexVo);

            couponList.add(couponIndexVo);
        }

        // floorGoodsList
        List<HomeIndexVo.FloorGoodsVo> floorGoodsList = new ArrayList<>();
        List<HomeIndexVo.GoodsIndexVo> goodsList = new ArrayList<>();
        String cateCount = systemMapper.selectByPrimaryKey(45).getKeyValue();
        String cateGoodsCount = systemMapper.selectByPrimaryKey(43).getKeyValue();
//        int cnt = 0;

        for (Category category : categories) {

            if (category.getPid() == 0){ // 父类目

                // id
                Integer id = category.getId();

                // name
                String name = category.getName();

                // goodsList
                GoodsExample example = new GoodsExample();
//                example.setOrderByClause("id" + " limit " + cateGoodsCount);
                example.createCriteria().andCategoryIdEqualTo(id).andDeletedEqualTo(false);
                List<Goods> goods = goodsMapper.selectByExampleWithBLOBs(example);
                for (Goods good : goods) {
                    HomeIndexVo.GoodsIndexVo goodsVo =  new HomeIndexVo.GoodsIndexVo();
                    BeanUtils.copyProperties(good, goodsVo);
                    goodsList.add(goodsVo);
                    if (goodsList.size() >= Integer.parseInt(cateGoodsCount)) break;
                }

                HomeIndexVo.FloorGoodsVo floorGoodsVo = new HomeIndexVo.FloorGoodsVo();
                floorGoodsVo.setId(id);
                floorGoodsVo.setName(name);
                floorGoodsVo.setGoodsList(goodsList);


                floorGoodsList.add(floorGoodsVo);
                if (floorGoodsList.size() >= Integer.parseInt(cateCount)) break;
            }

        }


        // grouponList，表是空的，不用写 =.=#
        List<HomeIndexVo.GrouponIndexVo> grouponList = null;


        // hotGoodsList
        String hotGoodsCount = systemMapper.selectByPrimaryKey(41).getKeyValue();

        GoodsExample hotGoodsExample = new GoodsExample();
        hotGoodsExample.setOrderByClause("id" + " limit " + hotGoodsCount);
        hotGoodsExample.createCriteria().andIsHotEqualTo(true).andDeletedEqualTo(false);
        List<Goods> hotGoods = goodsMapper.selectByExampleWithBLOBs(hotGoodsExample);
        List<HomeIndexVo.GoodsIndexVo> hotGoodsList = new ArrayList<>();

        for (Goods goods : hotGoods) {

            HomeIndexVo.GoodsIndexVo indexVo = new HomeIndexVo.GoodsIndexVo();
            BeanUtils.copyProperties(goods, indexVo);

            hotGoodsList.add(indexVo);
        }


        // newGoodsList
        String newGoodsCount = systemMapper.selectByPrimaryKey(34).getKeyValue();

        GoodsExample newGoodsExample = new GoodsExample();
        newGoodsExample.setOrderByClause("id" + " limit " + newGoodsCount);
        newGoodsExample.createCriteria().andIsNewEqualTo(true).andDeletedEqualTo(false);
        List<Goods> newGoods = goodsMapper.selectByExampleWithBLOBs(newGoodsExample);
        List<HomeIndexVo.GoodsIndexVo> newGoodsList = new ArrayList<>();

        for (Goods goods : newGoods) {

            HomeIndexVo.GoodsIndexVo indexVo = new HomeIndexVo.GoodsIndexVo();
            BeanUtils.copyProperties(goods, indexVo);

            newGoodsList.add(indexVo);
        }


        // topicList
        String topicCount = systemMapper.selectByPrimaryKey(48).getKeyValue();

        TopicExample topicExample = new TopicExample();
        topicExample.setOrderByClause("id" + " " + " limit " + topicCount);
//        topicExample.setLimit(Integer.parseInt(topicCount));
        topicExample.createCriteria().andDeletedEqualTo(false);
//        List<Topic> topics = topicMapper.selectByExampleWithBLOBs(topicExample);
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        List<HomeIndexVo.TopicIndexVo> topicList = new ArrayList<>();

        for (Topic topic : topics) {

            HomeIndexVo.TopicIndexVo topicIndexVo = new HomeIndexVo.TopicIndexVo();
            BeanUtils.copyProperties(topic, topicIndexVo);

            topicList.add(topicIndexVo);
        }



        // 封装返回vo
        HomeIndexVo vo = new HomeIndexVo();

        vo.setChannel(channel);
        vo.setBanner(banner);
        vo.setBrandList(brandList);
        vo.setCouponList(couponList);
        vo.setFloorGoodsList(floorGoodsList);
        vo.setGrouponList(grouponList);
        vo.setHotGoodsList(hotGoodsList);
        vo.setNewGoodsList(newGoodsList);
        vo.setTopicList(topicList);

        return vo;
    }
}
